/*
 * Created by SharpDevelop.
 * User: Bab
 * Date: 16/11/2006
 * Time: 10:44
 * 
 * Copyright 2006 POB-Technology. All rights reserved.
 * POB-Technology PROPRIETARY/CONFIDENTIAL.
 * 
 */

using System;
using System.Net;
using System.Net.Sockets;


namespace RgbComponent
{
	/// <summary>
	/// Simple socket tcp stream class.
	/// </summary>
	public class Ip : Communication
	{
		Socket s ;
		string addr ;
		int port ;
		
		public Ip( string address, int p )
		{
			s = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
			
			s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,3000);
		
			addr = address ;
			port = p ;
		}
		
		public void Open()
		{
			s.Connect( IPAddress.Parse(addr), port);
		}
		
		public void Write(byte []buffer,int len)
		{
			s.Send(buffer,len, SocketFlags.None);
		}
		
		public int Read(byte []buffer,int len)
		{
			int count = s.Receive(buffer,0,len, SocketFlags.None);
			
			return count ;
		}
		
		public void Close()
		{
			s.Close();
		}
		
	}
}
